﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Aliyun.OpenServices.OpenStorageService;
using System.Threading.Tasks;
using Top.Common;

namespace Top.Oss
{

    /// <summary>
    /// 客户OSS帮助类
    /// </summary>
    public sealed class OssHelper
    {
        private static OssUploadConfig uoloadConfig = null;
        private static OssClient client = null;

        /// <summary>
        /// 上传文件夹位置如FolderCategory/FolderRule
        /// </summary>
        public static string PreFolder
        {
            get;
            set;
        }

        private OssHelper()
        {
        }


        static OssHelper()
        {
            //获取client
            client = OssClientProvider.GetOssClient();

            //获取上传配置规则 可以从数据库、配置文件获取
            GetUploadConfig("Point");
        }

        private static void GetUploadConfig()
        {
            uoloadConfig = new OssUploadConfig()
            {
                FolderCategory = "ESF",
                FolderRule = "yyyyMM",
                BucketName = TopConfigHelper.GetAppSettings("OssBucketName")
            };

            PreFolder = uoloadConfig.FolderCategory + "/" + DateTime.Now.ToString(uoloadConfig.FolderRule) + "/";
        }

        private static void GetUploadConfig(string FolderCategoryName)
        {
            uoloadConfig = new OssUploadConfig()
            {
                FolderCategory = FolderCategoryName,
                FolderRule = "yyyyMMdd",
                BucketName = TopConfigHelper.GetAppSettings("OssBucketName")
            };

            PreFolder = uoloadConfig.FolderCategory + "/" + DateTime.Now.ToString(uoloadConfig.FolderRule) + "/";
        }

        #region 公共方法
        


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key">这里指需传文件名即可，文件夹由后台配置，如test.txt</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static PutObjectResult Upload(string bucketName, string key, string filePath)
        {
            Stream stream = File.OpenRead(filePath);

            return Upload(bucketName, key, stream);
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key">这里指需传文件名即可，文件夹由后台配置，如test.txt</param>
        /// <param name="content">文件流</param>
        /// <returns></returns>
        public static PutObjectResult Upload(string bucketName, string key, Stream content)
        {
            //没有指定，则取默认
            if (string.IsNullOrEmpty(bucketName))
            {
                bucketName = uoloadConfig.BucketName;
            }

            key = PreFolder + key;

            return client.PutObject(bucketName, key, content);
        }

        /// <summary>
        /// 异步上传
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static Task<PutObjectResult> UploadAsync(string bucketName, string key, Stream content)
        {
            //没有指定，则取默认
            if (string.IsNullOrEmpty(bucketName))
            {
                bucketName = uoloadConfig.BucketName;
            }

            key = PreFolder + key;

            return client.PutObjectAsync(bucketName, key, content);
        }


        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key"></param>
        public static void DeleteObj(string bucketName,string key)
        {
            //没有指定，则取默认
            if (string.IsNullOrEmpty(bucketName))
            {
                bucketName = uoloadConfig.BucketName;
            }

            client.DeleteObject(bucketName, key);
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key">文件完成路径</param>
        public static OssObject Download(string bucketName, string key)
        {
            //没有指定，则取默认
            if (string.IsNullOrEmpty(bucketName))
            {
                bucketName = uoloadConfig.BucketName;
            }

            return client.GetObject(bucketName, key);
        }

        /// <summary>
        /// 获取文件下载地址
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key">文件完成路径</param>
        /// <returns></returns>
        public static string GenerateDownloadUrl(string bucketName, string key)
        {
            return GenerateDownloadUrl(bucketName, key, null);
        }


        /// <summary>
        /// 获取文件下载地址
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="key">文件完成路径</param>
        /// <param name="expiration">过期时间</param>
        /// <returns></returns>
        public static string GenerateDownloadUrl(string bucketName, string key, DateTime? expiration)
        {
            //没有指定，则取默认
            if (string.IsNullOrEmpty(bucketName))
            {
                bucketName = uoloadConfig.BucketName;
            }

            Uri uri = null;

            if (expiration == null)
            {
                uri = client.GeneratePresignedUri(bucketName, key);
            }
            else
            {
                uri = client.GeneratePresignedUri(bucketName, key, expiration.Value);
            }

            return uri.AbsoluteUri;
        }




        #endregion
    }
}
